//
//  Weibo.m
//  hz89-weibo
//
//  Created by ZhuJiaCong on 16/6/18.
//  Copyright © 2016年 ZhuJiaCong. All rights reserved.
//

#import "Weibo.h"
#import "RegexKitLite.h"


@implementation Weibo

- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {
    
    NSArray *emotions = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"emoticons" ofType:@"plist"]];
    
    
    //正则表达式
    NSString *regex = @"\\[\\w+\\]";
    //微博正文
    NSString *weiboText = _text;
    //查找表情字符串
    NSArray *array = [weiboText componentsMatchedByRegex:regex];
    NSLog(@"%@", array);
    for (NSString *subString in array) {
        
        //使用谓词筛选对象
        NSString *string = [NSString stringWithFormat:@"chs = '%@'", subString];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:string];
        NSDictionary *eDic = [[emotions filteredArrayUsingPredicate:predicate] firstObject];
        
        //判断当前表情是否在本地表情包中
        if (eDic == nil) {
            continue;
        }
        
        NSString *emotion = eDic[@"png"];
        weiboText = [weiboText stringByReplacingOccurrencesOfString:subString withString:[NSString stringWithFormat:@"<image url = '%@'>", emotion]];
        
    }
    
    _text = weiboText;
    
    return YES;
}

@end
